package com.snake.vargoappstore.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.snake.mylibrary.base.BaseFragment;
import com.snake.mylibrary.loader.ArrayListAdapter;
import com.snake.mylibrary.loader.DynamicResult;
import com.snake.mylibrary.runtime.UiHandler;
import com.snake.vargoappstore.R;
import com.snake.vargoappstore.db.AppInfo;
import com.snake.vargoappstore.db.LocalAppInfo;
import com.snake.vargoappstore.loader.ManagerFragmentLeftLoader;
import com.snake.vargoappstore.result.ManagerFragmentLeftResult;
import com.snake.vargoappstore.widget.LocalListViewItem;


public class ManagerFragment extends BaseFragment {

    private View mView;
    private RadioGroup mRadioGroup;
    private ListView mListViewLeft;
    private ListView mListViewRight;
    private LeftAdapter mLeftAdapter;
    private RightAdapter mRightAdapter;
    private SwipeRefreshLayout mRefreshLeft;

    UiHandler mUiHandler = new UiHandler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    break;
            }
        }
    };
    private View mHeadView;


    @Override
    protected void initView() {
        mListViewLeft = (ListView) mView.findViewById(R.id.manager_left_listView);
        mListViewRight = (ListView) mView.findViewById(R.id.manager_right_listView);
        mRefreshLeft = (SwipeRefreshLayout) mView.findViewById(R.id.manager_left_swipeRefresh);
        mHeadView = View.inflate(getActivity(), R.layout.head_view, null);

    }

    @Override
    protected void initData() {
        mLeftAdapter = new LeftAdapter(getActivity());
        mRightAdapter = new RightAdapter(getActivity());
        mListViewLeft.setAdapter(mLeftAdapter);
        mListViewRight.setAdapter(mRightAdapter);
        mListViewLeft.addHeaderView(mHeadView);
    }

    @Override
    protected void initListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.manager_left_radioBtn:
                        mListViewLeft.setVisibility(View.VISIBLE);
                        mListViewRight.setVisibility(View.GONE);
                        getLoaderManager().restartLoader(0,null,new LeftLoaderCallBacks());
                        break;
                    case R.id.manager_right_radioBtn:
                        mListViewLeft.setVisibility(View.GONE);
                        mListViewRight.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
        mRadioGroup.check(R.id.manager_left_radioBtn);
        mRefreshLeft.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getLoaderManager().restartLoader(0,null,new LeftLoaderCallBacks());
            }
        });


    }

    @Override
    protected View getFragmentView() {
        mView = View.inflate(getActivity(), R.layout.manager_fragment, null);
        return mView;
    }

    class LeftAdapter extends ArrayListAdapter<LocalAppInfo>{

        public LeftAdapter(Context context) {
            super(context);
        }

        @Override
        public View onCreateView(Context context, ViewGroup parent) {
            return new LocalListViewItem(context);
        }

        @Override
        public void onBindData(View view, int position, LocalAppInfo localAppInfo) {
            LocalListViewItem item = (LocalListViewItem) view;
            item.bindData(position,localAppInfo);
        }
    }

    class RightAdapter extends ArrayListAdapter<AppInfo>{

        public RightAdapter(Context context) {
            super(context);
        }

        @Override
        public View onCreateView(Context context, ViewGroup parent) {
            return null;
        }

        @Override
        public void onBindData(View view, int position, AppInfo appInfo) {
        }
    }

    class LeftLoaderCallBacks implements LoaderManager.LoaderCallbacks<DynamicResult>{

        @Override
        public Loader<DynamicResult> onCreateLoader(int id, Bundle args) {
            mRefreshLeft.setRefreshing(true);
            return new ManagerFragmentLeftLoader(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<DynamicResult> loader, DynamicResult data) {
            ManagerFragmentLeftResult leftResult = (ManagerFragmentLeftResult) data;
            mLeftAdapter.updateData(leftResult.getLocalAppInfos());
            mRefreshLeft.setRefreshing(false);
        }

        @Override
        public void onLoaderReset(Loader<DynamicResult> loader) {

        }
    }

    class RightLoaderCallBacks implements LoaderManager.LoaderCallbacks<DynamicResult>{

        @Override
        public Loader<DynamicResult> onCreateLoader(int id, Bundle args) {
            return null;
        }

        @Override
        public void onLoadFinished(Loader<DynamicResult> loader, DynamicResult data) {

        }

        @Override
        public void onLoaderReset(Loader<DynamicResult> loader) {

        }
    }

    public void setRadioGroup(RadioGroup rg){
        mRadioGroup = rg;
    }

}
